// packageK/hxt/manage/manage.js
var o = require("../../../ec-canvas/echarts"),
  s = getApp();
  let myChart = null
  let myChart1 = null
  let myChart2 = null
  let myChart3 = null
  let myChart4 = null
  let myChart5 = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabInd: 0,
    time: -1,
    selecttime: false,
    area: [],
    selecAareaname:"",//省份
    selecAareanameId:"",//省份
    areaname:"",//省份
    areaid:"",
    ecFans: {
      lazyLoad: !0
    },
    istabone:0,
    option: {
      color: ['#377eef'],
      tooltip: {
        trigger: 'axis',
        triggerOn: 'click', //点击才会出现提示框
        backgroundColor: 'rgba(76, 151, 251,1)', // 背景颜色
        borderWidth: 0,
        axisPointer: {
          type: 'none'
        },
        textStyle: { // 文本样式
          color: '#fff', // 文本颜色
          fontSize: 14 // 文本大小
        },
        formatter: function (params) {
          // 只返回 x 轴的值
          return params[0].data;
        },
        confine: true,
        // position: function (point, params, dom, rect, size) {
        //     // point 是 [x, y] 格式
        //     // 可以自定义位置返回，例如：
        //     return [point[0] + 10, point[1] - 30]; // 在默认位置的基础上偏移
        // }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '0%',
        top: '5%',
        containLabel: true
      },
      xAxis: [{
        type: 'category',
        boundaryGap: false,
        data: [1, 2, 3, 4, 5, 6],
        axisTick: {
          show: true
        },
        axisLine: {
          show: false // 显示轴线
        },
      }],
      yAxis: [{
        type: 'value',
        axisLabel: {
          interval: 6 // 设置为6，表示只显示6个标签
        },
        axisTick: {
          show: false
        },
        axisLine: {
          show: false // 显示轴线
        },
      }],
      series: [{
        name: '',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 2
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new o.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgb(51, 123, 237)'
            },
            {
              offset: 1,
              color: 'rgb(255, 255, 255)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: [5, 20, 6, 16, 2, 46, 55]
      }]
    },
    uid:"",
    order_count:"0",
    price_sum:"0",
    money:"0",
    orders:"0",
    reward:"0",
    agencyInfo:[],
    optionsChartArr:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initEcharts()
    this.setData({
      uid:wx.getStorageSync('yz_uid')
    })
    var t = s.getNetAddresss("Baoxian.getProvince");
    let e = this
    s._postNetWork({
        url: t,
        success(l){
            let i = l.data
            if(i.result == 1){
              e.setData({
                area:i.data,
                areaname: i.data[0].areaname,
                selecAareaname: i.data[0].areaname,
                selecAareanameId:i.data[0].id,
                areaid:i.data[0].id,
              })
              e.getProvinceInfo(i.data[0].id)
              e.getManpower(i.data[0].id)
            }
        }
    })
  },
  selectTab(e) {
    let t = e.currentTarget.dataset.tab
    if (this.data.tabInd == t) return
    this.setData({
      tabInd: t
    })
    if(t == 1 && this.data.istabone == 0){
      this.initLineChart()
      this.data.istabone = 1
    }
  },
  // 选择省份
  selectDay(e) {
    let t = e.currentTarget.dataset.time
    this.setData({
      areaname: t.areaname,
      areaid:t.id
    })
  },
  // 选择省份
  selectdayshow(e) {
    this.setData({
      selecttime: !this.data.selecttime
    })
    let t = e.currentTarget.dataset.con

    if(t == "y"){
      this.setData({
        selecAareaname: this.data.areaname,
        selecAareanameId:this.data.areaid,
        istabone:0
      })
      this.getProvinceInfo(this.data.selecAareanameId)
      this.getManpower(this.data.selecAareanameId)
    }else{
      this.setData({
        areaname: this.data.selecAareaname,
        areaid:this.data.selecAareanameId
      })
    }
  },
  getProvinceInfo(str){
    var t = s.getNetAddresss("Baoxian.provinceInfo");
    let e = this
    s._postNetWork({
        url: t,
        data:{
          province:str
        },
        success(l){
            let i = l.data
            if(i.result == 1){
              let arr = []
              for (const key in i.data.agencyInfo) {
                if (Object.hasOwnProperty.call(i.data.agencyInfo, key)) {
                  const element = i.data.agencyInfo[key];
                  arr.push({
                    level_name:element.level_name,
                    money:e.convert(element.money),
                    orders:e.convert(element.orders),
                    reward:e.convert(element.reward)
                  })
                }
              }
              e.setData({
                money:e.convert(i.data.statistics.money),
                orders: e.convert(i.data.statistics.orders),
                reward: e.convert(i.data.statistics.reward),
                agencyInfo:arr

              })
            }
        }
    })
  },
  getManpower(str){
    var t = s.getNetAddresss("Baoxian.manpower");
    let e = this
    s._postNetWork({
        url: t,
        data:{
          province:str
        },
        success(l){
            let i = l.data
            if(i.result == 1){
              let arr = []
              for (const key in i.data) {
                if (Object.hasOwnProperty.call(i.data, key)) {
                  const element = i.data[key];
                  let o = JSON.parse(JSON.stringify(e.data.option))
                  // o.tooltip.formatter = function (params) {
                  //   // 只返回 x 轴的值
                  //   return params[0].data;
                  // },
                  o.xAxis[0].data = element.map(item=>item.date)
                  o.series[0].data = element.map(item=>item.complete)
                  let data = {
                    name:key,
                    options:o
                  }
                  arr.push(data)
                }
              }

              e.setData({
                optionsChartArr:arr
              })
            }
        }
    })
  },
  convert(num){
    if(num > 0){
      let n = num.split(".")[0]
      if(n.length>4){
        let w = String(n).substring(0,3)
        return w + "万"
      }else{
        return num
      }
    } else{
      return num
    }
  },
  initEcharts(type) {
    myChart = this.selectComponent('#myChart1')
    myChart1 = this.selectComponent('#myChart2')
    myChart2 = this.selectComponent('#myChart3')
    myChart3 = this.selectComponent('#myChart4')
    myChart4 = this.selectComponent('#myChart5')
    myChart5 = this.selectComponent('#myChart6')
    var t = s.getNetAddresss("Baoxian.basicData");
    let e = this
    s._postNetWork({
        url: t,
        success(l){
            let i = l.data
            if(i.result == 1){
              e.setData({
                order_count:i.data.order_count,
                price_sum:i.data.price_sum
              })
              let opt = JSON.parse(JSON.stringify(e.data.option))
              opt.xAxis[0].data = i.data.chart_data.map(item=>item.date)
              opt.series[0].data = i.data.chart_data.map(item=>item.complete)
              myChart.init((canvas, width, height, dpr)=>{
                const chart = o.init(canvas,null,{ width, height, dpr})
                chart.setOption(opt)
                return chart
              })
            }
        }
    })
    
    
  },
  // 生成折线图
  initLineChart(){
    
    myChart1.init((canvas, width, height, dpr)=>{
      const chart = o.init(canvas,null,{ width, height, dpr})
      chart.setOption(this.data.optionsChartArr[0].options)
      return chart
    })
    myChart2.init((canvas, width, height, dpr)=>{
      const chart = o.init(canvas,null,{ width, height, dpr})
      chart.setOption(this.data.optionsChartArr[1].options)

      return chart
    })
    myChart3.init((canvas, width, height, dpr)=>{
      const chart = o.init(canvas,null,{ width, height, dpr})
      chart.setOption(this.data.optionsChartArr[2].options)

      return chart
    })
    myChart4.init((canvas, width, height, dpr)=>{
      const chart = o.init(canvas,null,{ width, height, dpr})
      chart.setOption(this.data.optionsChartArr[3].options)
      return chart
    })
    myChart5.init((canvas, width, height, dpr)=>{
      const chart = o.init(canvas,null,{ width, height, dpr})
      chart.setOption(this.data.optionsChartArr[4].options)
      return chart
    })
    
  },
  // 生成图片
  createImage(){
    const query = wx.createSelectorQuery();
    // 选择要转换的指定元素
    query.select('.content').boundingClientRect((rect) => {
      console.log(rect);
      // query.select('#myCanvas').node((res) => {
      //   console.log(res);
      // }).exec()
      // canvas.canvas.width = rect.width;
      // canvas.canvas.height = rect.height;
      // // 绘制背景
      // canvas.setFillStyle('white');
      // canvas.fillRect(0, 0, rect.width, rect.height);
      // // 绘制图片
      // const img = canvas.createImage();
      // console.log(img);
    }).exec()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})